const app = getApp()
const { addApi } = require('../../../api/services/yhAdd/index')

const util = require('../../../utils/util.js')

Page({
  data: {
    troubleId: '',
    troubleState: '',
    checkBasis: '',
    troubleSite: '',
    troubleSuggest: '',
    troubleType: '',
    troubleClass: '',
    okContent: '',
    okDate: '',
    checkUrl: '',
    isOk: 1,

    show: false,
    currentDate: new Date().getTime(),
    minDate: new Date().getTime(),
    formatter(type, value) {
      if (type === 'year') {
        return `${value}年`
      }
      if (type === 'month') {
        return `${value}月`
      }
      return value
    },
    fileList: [],
  },

  onChange(event) {
    console.log(event.detail)
    this.setData({
      isOk: event.detail,
    })
  },

  // 时间
  handleConfirm(event) {
    this.setData({
      okDate: util.formatTime(new Date(event.detail)),
      show: false,
    })
  },

  showPopup() {
    this.setData({ show: true })
  },

  // 时间弹窗
  onCloseTime() {
    this.setData({ show: false })
  },

  async handleSumbit() {
    let params = {
      troubleId: this.data.troubleId,
      troubleState: this.data.troubleState,
      checkBasis: this.data.checkBasis,
      troubleSite: this.data.troubleSite,
      troubleSuggest: this.data.troubleSuggest,
      troubleType: this.data.troubleType,
      troubleClass: this.data.troubleClass,
      okContent: this.data.okContent,
      okDate: this.data.okDate,
      isOk: this.data.isOk,
      checkUrl: this.data.checkUrl,
    }
    const res = await addApi(params)
    console.log(res)
    if (res.code != 200) {
      return app.toast(res.msg)
    }
    wx.navigateBack({
      delta: 1, //返回上一级页面
    })
  },

  // 图片上传

  afterRead(event) {
    const { file } = event.detail
    console.log(file)
    let _this = this
    wx.uploadFile({
      url: 'https://dy.microzl.com/sq-smart-campus-api/xt-system/sysfile/uploadFile', // 仅为示例，非真实的接口地址
      filePath: file.url,
      name: 'file',
      header: {
        'Content-Type': 'multipart/form-data',
        Authorization: 'Bearer ' + wx.getStorageSync('token'),
      },

      success(res) {
        _this.setData({
          fileList: [
            {
              url: '',
            },
          ],
        })

        let data = JSON.parse(res.data)
        _this.setData({ fileList: [{ url: data.data.fUrl }], checkUrl: data.data.fUrl })
      },
      fail(err) {
        console.log(object)
      },
    })
  },

  // 隐患类型：
  handleType() {
    wx.navigateTo({
      url: '/pages/safety/spTroubleType/spTroubleType',
    })
  },
  // 隐患分类：
  handleClass() {
    wx.navigateTo({
      url: '/pages/safety/spTroubleClass/spTroubleClass',
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log('onload')
    console.log(options)
    this.setData({
      troubleId: options.troubleId,
    })
  },
})
